package com.endoc.news.fragment.newslist;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.endoc.news.fragment.base.BaseMvvmModel;
import com.endoc.news.fragment.base.BaseMvvmViewModel;
import com.endoc.news.fragment.base.BaseViewModel;
import com.endoc.news.fragment.mvvm.model.IBaseModelListener;
import com.endoc.news.fragment.mvvm.model.PagingResult;
import com.endoc.news.fragment.newslist.view.ViewStatus;

import java.util.ArrayList;
import java.util.List;

public class NewsFragmentViewModel  extends BaseMvvmViewModel{
    //MutableLiveData<List<BaseViewModel>> mBaseViewModelsMutableLiveData= new MutableLiveData<List<BaseViewModel>>();
    //NewsListBeanModel mNewsListBeanModel;
    //MutableLiveData<ViewStatus>  mViewStatusMutableLiveData =new MutableLiveData<>();
    //public MutableLiveData<String> errorMessage = new MutableLiveData<>();
    String mChannelId;
    String mChannelName;
    public  NewsFragmentViewModel(String channelId,String channelName){
        mChannelId =channelId;
        mChannelName =channelName;
        //mNewsListBeanModel =  new NewsListBeanModel(channelId,channelName);
        //mNewsListBeanModel.registerModelListener(this);
        //mBaseViewModelsMutableLiveData.setValue(new ArrayList<BaseViewModel>());//初始化一个list,否则后面清除和添加的时候会崩溃
        //mNewsListBeanModel.getCacheData();
    }

    @Override
    public BaseMvvmModel createModel() {
        return  new NewsListBeanModel(mChannelId,mChannelName);
    }

    /**
     * ViewModelProvider工厂,创建ViewModel对象
     */
   public  static class NewsFragmentViewModelFactory implements ViewModelProvider.Factory{

       private String mChannelId;
       private String mChannelName;
       public NewsFragmentViewModelFactory(String channelId, String channelName) {
           this.mChannelId = channelId;
           this.mChannelName = channelName;
       }
       @NonNull
       @Override
       public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
           return  (T) new NewsFragmentViewModel(mChannelId, mChannelName);
       }
   }

 /*   public void refresh(){
        try {
            mNewsListBeanModel.refresh();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

 /*   public void loadNext(){
        try {
            mNewsListBeanModel.loadNextPage();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/



    /*@Override
    public void success(BaseMvvmModel baseMvvmModel, List<BaseViewModel> t, PagingResult... pagingResults) {
        if(baseMvvmModel instanceof NewsListBeanModel){
            if(pagingResults[0].isFirstPage){//如果是第一页
                mBaseViewModelsMutableLiveData.getValue().clear();
            }
            if(pagingResults[0].isEmpty){//如果为空,说明没有数据,解析失败
                if(pagingResults[0].isFirstPage){//如果是第一页,说明没有一页有数据
                    mViewStatusMutableLiveData.postValue(ViewStatus.EMPTY);
                }else {
                    //如果是其他页,说明没有更多的数据了
                    mViewStatusMutableLiveData.postValue(ViewStatus.NO_MORE_DATA);
                }
            }else {
                if(pagingResults[0].isFirstPage){//如果是第一页,那么直接用这次数据
                    mBaseViewModelsMutableLiveData.postValue(t);
                }else {//如果不是第一页
                    mBaseViewModelsMutableLiveData.getValue().addAll(t);//加入集合
                    mBaseViewModelsMutableLiveData.postValue(mBaseViewModelsMutableLiveData.getValue());
                }
            }
        }

      *//*  if(pagingResults!=null&&pagingResults.length>0&&pagingResults[0].isFirstPage){
            mBaseViewModels.clear();
        }
        mBaseViewModels.addAll(t);
        //((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(baseViewModelList);
        ((NewsRecycleAdapter)fragmentNewsBinding.recycler.getAdapter()).setData(mBaseViewModels);
        fragmentNewsBinding.srl.finishLoadMore();
        fragmentNewsBinding.srl.finishRefresh();*//*
    }
*/
  /*  @Override
    public void fail(BaseMvvmModel baseMvvmModel, Throwable e, PagingResult... pagingResults) {
        errorMessage.postValue(e.getMessage());
        if(pagingResults[0].isFirstPage){//如果是第一页,说明没有一页有数据
            mViewStatusMutableLiveData.postValue(ViewStatus.REFRESH_ERROR);
        }else {
            //如果是其他页,说明没有更多的数据了
            mViewStatusMutableLiveData.postValue(ViewStatus.NO_MORE_DATA);
        }

       *//* fragmentNewsBinding.srl.finishLoadMore();
        fragmentNewsBinding.srl.finishRefresh();*//*
    }*/
}
